/*
 * anima/x86_64/thread.cc - Thread functions for x86-64 architecture.
 * Copyright (C) 2007  Mikhail Vorozhtsov
 * See the LICENSE section of the README file for details.
 */

/* $Id$ */

#include HEADER (anima,thread.hh)
#include HEADER (anima,cpu.hh)
#include HEADER (anima,pdb.hh)
#include HEADER (anima,init.hh)
#include HEADER (anima,space.hh)

START_NAMESPACE (anima)

bool
thread_t::init_instance (space_t *space UNUSED)
{
  pdb::lvl1e_t *sc_stack_lvl1e = NULL;

  if (init::done_p ())
    {
    }
  else
    sc_stack_lvl1e
      = init::get_main_space ().get ()->allocate (pdb::entry_t::USAGE_THREAD);

  if (!sc_stack_lvl1e)
    return false;

  sc_stack_pte.set_raw (0).set_base (sc_stack_lvl1e->entry.base)
    .set_NX ().set_RW ().set_P ();

  //cpu_ctx.flags = 0;
  cpu_ctx.cs = cpu_t::SELECTOR_USER_CODE;
  cpu_ctx.ss = cpu_t::SELECTOR_USER_DATA;
  cpu_ctx.rsp = 0;

  return true;
}

void
thread_t::set_ip (u64_t addr)
{
  cpu_ctx.rip = addr;
}

END_NAMESPACE

